package com.whfc.openapi.api.face.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.enums.RecMode;
import com.whfc.common.enums.SyncType;
import com.whfc.common.face.fcr.enums.FcrOp;
import com.whfc.common.face.fcr.param.FcrDoor;
import com.whfc.common.face.fcr.param.FcrRecordParam;
import com.whfc.common.face.fcr.param.FcrSyncDoorParam;
import com.whfc.common.face.fcr.param.FcrSyncEmpParam;
import com.whfc.common.face.wheatSunshine.RecCallBack;
import com.whfc.common.face.yunqi.EmpSyncParam;
import com.whfc.common.face.yunqi.FaceGateSyncParam;
import com.whfc.common.face.yunqi.RecSyncParam;
import com.whfc.common.util.DateUtil;
import com.whfc.emp.constant.EmpConstant;
import com.whfc.emp.enums.AppWorkRole;
import com.whfc.emp.enums.EmpSyncImgType;
import com.whfc.emp.enums.FaceGateSyncOp;
import com.whfc.emp.enums.GenderCode;
import com.whfc.emp.param.*;
import com.whfc.emp.param.openapi.OpenApiEmpSyncParam;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 闸机数据转换
 *
 * @author qzexing
 * @version 1.0
 * @date 2021-08-06 16:16
 */
public class FaceGateDataConverter {


    /**
     * 德宝考勤数据转换
     *
     * @param faceGateRecordParam 德宝闸机考勤数据
     * @return 考勤数据
     */
    public static EmpAttendSyncDataParam attendDataConverter(FaceGateRecordParam faceGateRecordParam) {
        if (faceGateRecordParam == null) {
            return null;
        }
        EmpAttendSyncDataParam dataParam = new EmpAttendSyncDataParam();
        dataParam.setPersonGuid(faceGateRecordParam.getPersonGuid());
        dataParam.setDeviceKey(faceGateRecordParam.getDeviceKey());
        dataParam.setEmpName(faceGateRecordParam.getEmpName());
        dataParam.setShowTime(faceGateRecordParam.getShowTime());
        dataParam.setRecMode(faceGateRecordParam.getRecMode());
        //图片
        dataParam.setImgType(EmpSyncImgType.BASE64.getValue());
        dataParam.setPicture(faceGateRecordParam.getData());
        return dataParam;
    }


    /**
     * 玉川考勤数据转换
     *
     * @param fcrRecordParam 玉川闸机考勤数据
     * @return 考勤数据
     */
    public static EmpAttendSyncDataParam attendDataConverter(FcrRecordParam fcrRecordParam) {
        if (fcrRecordParam == null) {
            return null;
        }
        EmpAttendSyncDataParam dataParam = new EmpAttendSyncDataParam();
        dataParam.setPersonGuid(fcrRecordParam.getUserNid());
        dataParam.setIdCardNo(fcrRecordParam.getIDCardSN());
        dataParam.setDeviceKey(fcrRecordParam.getDoorDid());
        dataParam.setEmpName(fcrRecordParam.getUserName());
        Date time = DateUtil.parseDate(fcrRecordParam.getPassTime(), "yyyy-MM-dd HH:mm:ss");
        dataParam.setShowTime(time);
        dataParam.setRecMode(RecMode.FACE.getValue());
        //图片
        dataParam.setImgType(EmpSyncImgType.BASE64.getValue());
        dataParam.setPicture(fcrRecordParam.getPassImageBase64());
        return dataParam;
    }

    /**
     * 麦管家考勤数据转换
     *
     * @param recCallBack 麦管家闸机考勤数据
     * @return 考勤数据
     */
    public static EmpAttendSyncDataParam attendDataConverter(RecCallBack recCallBack) {
        if (recCallBack == null) {
            return null;
        }
        EmpAttendSyncDataParam dataParam = new EmpAttendSyncDataParam();
        dataParam.setPersonGuid(recCallBack.getGuid());
        dataParam.setDeviceKey(recCallBack.getDeviceKey());
        dataParam.setPicture(recCallBack.getPicture());
        dataParam.setShowTime(new Date(recCallBack.getClockTime()));
        dataParam.setRecMode(RecMode.FACE.getValue());
        dataParam.setTemperature(recCallBack.getTemperature());
        return dataParam;
    }


    /**
     * 宇泛离线考勤数据转换
     *
     * @param content 宇泛闸机考勤数据
     * @return 考勤数据
     */
    public static EmpAttendSyncDataParam attendDataConverter(String content) {
        JSONObject jsonObject = JSON.parseObject(content);
        if (jsonObject == null) {
            return null;
        }
        EmpAttendSyncDataParam dataParam = new EmpAttendSyncDataParam();
        dataParam.setPersonGuid(jsonObject.getString("empNo"));
        String deviceKey = jsonObject.getString("deviceKey");
        dataParam.setDeviceKey(deviceKey);
        dataParam.setPicture("");
        Date date = new Date();
        Long time = jsonObject.getLong("recTime");
        if (time != null) {
            date = new Date(time);
        }
        dataParam.setShowTime(date);
        dataParam.setRecMode(RecMode.FACE.getValue());
        dataParam.setDeviceKeyList(Collections.singletonList(deviceKey));
        return dataParam;
    }

    /**
     * 云启考勤数据转换
     *
     * @param recSyncParam 云启闸机考勤数据
     * @return 考勤数据
     */
    public static EmpAttendSyncDataParam attendDataConverter(RecSyncParam recSyncParam) {
        if (recSyncParam == null) {
            return null;
        }
        EmpAttendSyncDataParam dataParam = new EmpAttendSyncDataParam();
        String personGuid = recSyncParam.getPersonGuid() == null ? recSyncParam.getEmpName() : recSyncParam.getPersonGuid();
        dataParam.setPersonGuid(personGuid);
        dataParam.setEmpName(recSyncParam.getEmpName());
        dataParam.setDeviceKey(recSyncParam.getDeviceKey());
        dataParam.setShowTime(recSyncParam.getTime());
        dataParam.setRecMode(recSyncParam.getRecMode());
        dataParam.setTemperature(recSyncParam.getTemperature());
        //图片
        dataParam.setImgType(EmpSyncImgType.BASE64.getValue());
        dataParam.setPicture(recSyncParam.getPicture());
        //云启需要触发同步事件
        dataParam.setTriggerEvent(EmpConstant.FACE_GATE_TRIGGER_EVENT);
        return dataParam;
    }

    /**
     * 德宝人员信息数据转换
     *
     * @param empParam 德宝人员信息
     * @return 人员信息
     */
    public static EmpInfoSyncDataParam empInfoDataConverter(DeBaoSyncEmpParam empParam) {
        if (empParam == null) {
            return null;
        }
        EmpInfoSyncDataParam dataParam = new EmpInfoSyncDataParam();
        dataParam.setEmpCode(empParam.getEmpCode());
        dataParam.setEmpName(empParam.getEmpName());
        dataParam.setIdCardNo(empParam.getIdCardNo());
        dataParam.setGender(empParam.getSex());
        dataParam.setNation(empParam.getNation());
        String workRoleId = empParam.getWorkRoleId();
        if (StringUtils.isNotBlank(workRoleId)) {
            AppWorkRole appWorkRole = "10".equals(workRoleId) ? AppWorkRole.MANAGER : AppWorkRole.WORKER;
            dataParam.setWorkRoleId(appWorkRole.getValue());
            dataParam.setWorkRoleName(appWorkRole.getDesc());
        }
        dataParam.setGroupCode(empParam.getGroupId());
        dataParam.setWorkTypeName(empParam.getWorkTypeName());
        dataParam.setSyncType(FaceGateSyncOp.EMP_ADD_OR_UPDATE.getValue());
        //图片类型
        dataParam.setAvatar(empParam.getAvatar());
        dataParam.setImgType(EmpSyncImgType.BASE64.getValue());
        return dataParam;
    }

    /**
     * 德宝人员信息数据转换
     *
     * @param empParam 德宝人员信息
     * @return 人员信息
     */
    public static EmpInfoSyncDataParam empInfoDataConverter(EmpSyncParam empParam) {
        if (empParam == null) {
            return null;
        }
        EmpInfoSyncDataParam dataParam = new EmpInfoSyncDataParam();
        dataParam.setEmpCode(empParam.getPersonGuid());
        dataParam.setEmpName(empParam.getEmpName());
        dataParam.setIdCardNo(empParam.getIdCardNo());
        dataParam.setGender(empParam.getGender());
        dataParam.setAvatar(empParam.getAvatar());
        dataParam.setNation(empParam.getNation());
        dataParam.setPhone(empParam.getPhone());
        dataParam.setIdCardNo(empParam.getIdCardNo());
        dataParam.setHeadImg(empParam.getHeadImg());
        dataParam.setAddress(empParam.getAddress());
        dataParam.setGroupName(empParam.getGroupName());
        //同步类型转换  将syncType 转换为  FaceGateSyncOp
        //syncType  1-新增  2-修改  3-删除
        if (SyncType.ADD.getValue().equals(empParam.getSyncType())
                || SyncType.UPDATE.getValue().equals(empParam.getSyncType())) {
            dataParam.setSyncType(FaceGateSyncOp.EMP_ADD_OR_UPDATE.getValue());
        } else if (SyncType.DELETE.getValue().equals(empParam.getSyncType())) {
            dataParam.setSyncType(FaceGateSyncOp.EMP_DEL.getValue());
        }
        //图片类型
        dataParam.setImgType(EmpSyncImgType.BASE64.getValue());
        return dataParam;
    }

    /**
     * 玉川人员同步
     *
     * @param empParam 玉川人员信息
     * @param op       操作类型
     * @return 人员信息
     */
    public static EmpInfoSyncDataParam empInfoDataConverter(FcrSyncEmpParam empParam, String op) {
        if (empParam == null) {
            return null;
        }
        EmpInfoSyncDataParam dataParam = new EmpInfoSyncDataParam();
        dataParam.setEmpName(empParam.getName());
        dataParam.setEmpCode(empParam.getNid());
        dataParam.setIdCardNo(empParam.getIDCardSN());
        dataParam.setPhone(empParam.getTelephone());
        //性别
        int gender = 0;
        if (GenderCode.man.getDesc().equals(empParam.getSex())) {
            gender = 1;
        } else if (GenderCode.woman.getDesc().equals(empParam.getSex())) {
            gender = 2;
        }
        dataParam.setGender(gender);
        dataParam.setNation(empParam.getNation());
        dataParam.setAddress(empParam.getAddress());
        dataParam.setAvatar(empParam.getFaceImageBase64());
        dataParam.setHeadImg(empParam.getIDCardImageBase64());
        dataParam.setImgType(EmpSyncImgType.BASE64.getValue());
        //人员授权闸机
        List<FcrDoor> perList = empParam.getPerList();
        if (perList != null && !perList.isEmpty()) {
            List<String> deviceKeyList = perList.stream().map(FcrDoor::getDoorDid).collect(Collectors.toList());
            dataParam.setDeviceKeyList(deviceKeyList);
        }
        //人员操作类型 新增/修改、删除
        if (FcrOp.EMP.getValue().equals(op)) {
            dataParam.setSyncType(FaceGateSyncOp.EMP_ADD_OR_UPDATE.getValue());
        } else if (FcrOp.DEL_EMP.getValue().equals(op)) {
            dataParam.setSyncType(FaceGateSyncOp.EMP_DEL.getValue());
        }
        return dataParam;
    }

    /**
     * 宇泛人员同步
     *
     * @param empParam 宇泛人员信息
     * @return 人员信息
     */
    public static EmpInfoSyncDataParam empInfoDataConverter(OpenApiEmpSyncParam empParam) {
        if (empParam == null) {
            return null;
        }
        EmpInfoSyncDataParam dataParam = new EmpInfoSyncDataParam();
        dataParam.setEmpCode(empParam.getEmpId());
        dataParam.setEmpName(empParam.getName());
        dataParam.setPhone(empParam.getPhone());
        dataParam.setIdCardNo(empParam.getIdCardNo());
        dataParam.setGender(empParam.getGender());
        Long time = empParam.getEntryTime();
        Date enterTime = new Date();
        if (time != null) {
            enterTime = new Date(time);
        }
        dataParam.setEnterTime(enterTime);
        return dataParam;
    }


    /**
     * 德宝班组信息数据转换
     *
     * @param groupParam 德宝班组信息
     * @return 班组信息
     */
    public static EmpGroupSyncDataParam groupDataConverter(DeBaoSyncGroupParam groupParam) {
        if (groupParam == null) {
            return null;
        }
        EmpGroupSyncDataParam dataParam = new EmpGroupSyncDataParam();
        dataParam.setGroupCode(groupParam.getGroupCode());
        dataParam.setGroupName(groupParam.getGroupName());
        return dataParam;
    }

    /**
     * 云启设备信息数据转换
     *
     * @param deviceParam 设备数据
     * @return 设备数据
     */
    public static EmpDeviceSyncDataParam deviceDataConverter(FaceGateSyncParam deviceParam) {
        if (deviceParam == null) {
            return null;
        }
        EmpDeviceSyncDataParam dataParam = new EmpDeviceSyncDataParam();
        dataParam.setDeviceKey(deviceParam.getDeviceKey());
        dataParam.setDeviceName(deviceParam.getDeviceKey());
        dataParam.setDirection(deviceParam.getDirection());
        //同步类型转换  将syncType 转换为  FaceGateSyncOp
        //syncType  1-新增  2-修改  3-删除
        if (SyncType.ADD.getValue().equals(deviceParam.getSyncType())
                || SyncType.UPDATE.getValue().equals(deviceParam.getSyncType())) {
            dataParam.setSyncType(FaceGateSyncOp.DEVICE_ADD_OR_UPDATE.getValue());
        } else if (SyncType.DELETE.getValue().equals(deviceParam.getSyncType())) {
            dataParam.setSyncType(FaceGateSyncOp.DEVICE_DEL.getValue());
        }
        return dataParam;
    }

    /**
     * 玉川设备同步
     *
     * @param deviceParam 玉川设备信息
     * @return 设备信息
     */
    public static EmpDeviceSyncDataParam deviceDataConverter(FcrSyncDoorParam deviceParam) {
        if (deviceParam == null) {
            return null;
        }
        EmpDeviceSyncDataParam dataParam = new EmpDeviceSyncDataParam();
        dataParam.setDeviceKey(deviceParam.getDid());
        dataParam.setDeviceName(deviceParam.getName());
        //玉川进出方向转换
        // 风潮进出方向  0-出  1-进
        // 玉川进出方向  0-进  1-出
        Integer inType = deviceParam.getInType();
        int direction = 0;
        if (0 == inType) {
            direction = 1;
        }
        dataParam.setDirection(direction);
        dataParam.setSyncType(FaceGateSyncOp.DEVICE_ADD_OR_UPDATE.getValue());
        return dataParam;
    }


}
